<?php

///////////////////////////////////////////////////////////////////////////////
// 
// JK_WEATHER
//
// This script gets weather data from the Environment of Canada, formats it, and inserts it
//
///////////////////////////////////////////////////////////////////////////////

// get data from Environment Canada and parse
// for parsing xml with CDATA see:
// http://www.vijayjoshi.org/2009/09/22/quick-php-tip-how-to-parse-cdata-sections-using-simplexml/
$xml = simplexml_load_file('http://www.weatheroffice.gc.ca/rss/city/ab-50_e.xml');
// for parsing in php with simplexml see:
// http://stackoverflow.com/questions/1575788/php-library-for-parsing-xml-with-a-colons-in-tag-names
// http://stackoverflow.com/questions/1893024/basic-simplexml-working-example
$weather_raw = $xml->children()->channel->item[1]->title;

// extract temperature string
$weather_raw_split = explode(",", $weather_raw);
$temp = $weather_raw_split[1];
$temp_float = (float) substr($temp,0,-3);

// get temp style
if ( $temp_float > 25 ) {
	$temp_style = "HeaderTickerHot";
	}
else if ( $temp_float < -10 ) {
	$temp_style = "HeaderTickerCold";
} else {
	$temp_style = "";
}

// write formatted string
$out_array = array("<span class=\"", $temp_style, "\">", $temp, "</span>");
echo join($out_array);

?>